
core.Desktop = function(aParams){
	this.init(aParams);
}

core.extendClass('core.Desktop', 'core.Box');

core.Desktop.prototype.init = function(aParams){
	this.xType = 'desktop';	
	$(this.div).addClass('desktop');
	this.padding = 5;
	this.dialogs = [];
	this.workspaces = [];
	var instance = this;
	this.taskbar = new core.Tabs({
		
	});
	this.body = new core.Panel({
		
	});
	this.superCall('init', arguments, {
		items: [this.body, this.taskbar],
		columns: '1',
		rows: '1, d'
	});
    this.padding = 0;
	this.taskbar.on('select', function(i, e, tab){
        instance.showWorkspace(tab.workspace);
//		core.log('tab selected', tab, tab.workspace.name, tab.workspace.root.instanceID);
//		instance.replaceChild(instance.items[0], tab.workspace.root, core.REPLACE_CHILD_HIDE);
//        tab.workspace.root.show();
//		tab.workspace.showFrames();
//		instance.refresh(true);
	});
}

core.WSTab = function(config){
	this.init(config);
}

core.extendClass('core.WSTab', 'core.Tab');

core.WSTab.prototype.onTimeout = function(aDraggable){
//	core.log(this.dropableCandidate, aDraggable);
	if(this.dropableCandidate==aDraggable && this.markedAsDropable){
//		core.log('ready to switch ws', this);
		core.desktop.taskbar.selectTab(this);
	}
}

core.WSTab.prototype.getDropTarget = function(aDraggable){
	if(this.timeout)
		core.clearTimeout(this.timeout);
	if(this.selected)
		return false;
    if(this.workspace.hasFrame(aDraggable.frame))
        return false;
	this.timeout = core.setTimeout(this, this.onTimeout, 1000, aDraggable);
	return this;
}

core.Desktop.prototype.showWorkspace = function(aWorkspace){
	for(var i = 0; i<this.workspaces.length; i++){
		if(this.workspaces[i]==aWorkspace){
            this.replaceChild(this.items[0], aWorkspace.root, core.REPLACE_CHILD_HIDE);
            aWorkspace.root.show();
            this.taskbar.selectTab(aWorkspace.taskbarTab);
            aWorkspace.showFrames();
            this.refresh(true);
            return true;
        }
	}
	return false;
}

core.Desktop.prototype.addWorkspace = function(aWorkspace){
	for(var i = 0; i<this.workspaces.length; i++){
		if(this.workspaces[i]==aWorkspace)
			return false;
	}
	this.workspaces.push(aWorkspace);
	var tab = new core.WSTab({
		text: aWorkspace.title || aWorkspace.name,
        closeable: true
	});
	tab.workspace = aWorkspace;
	aWorkspace.taskbarTab = tab;
	this.taskbar.addTab(tab);
    return true;
};

core.Desktop.prototype.setFocusedPanel = function(aPanel){
//    core.log('Set focused panel', aPanel.instanceID);
    if(this.focusedPanel==aPanel)
        return;
    if(this.focusedPanel)
        this.focusedPanel.unfocus();
    this.focusedPanel = aPanel;
}

core.Desktop.prototype.processKeyDown = function(e){
	if(this.dialogs.length==0)
		return this.superCall('processKeyDown', arguments, e);
	return this.dialogs[this.dialogs.length-1].processKeyDown(e);
}

core.Desktop.prototype.closeDialog = function(aDialog){
	if(this.dialogs.length==0)
		return;
	for(var i = 0; i<this.dialogs.length; i++){
		if(this.dialogs[i]==aDialog){
			this.dialogs.splice(i, 1);
			$(aDialog.div).remove();
			break;
		}
	}
//	core.log(this.dialogs.length);
	if(this.dialogs.length==0){
		$(this.dDiv).remove();
		this.dDiv = null;
	}
}
core.Desktop.prototype.showDialog = function(aDialog){
	if(this.dialogs.length==0){
		//Create dim div
		this.dDiv = core.newDiv(this.div, 'desktop_disable');
		$(this.dDiv).mousedown(function(){
			return false;
		});
//		$(this.dDiv).width(this.bounds.width);
//		$(this.dDiv).height(this.bounds.height);
//		$(this.dDiv).fadeTo(1, 0.2);
	}
	this.reParent(aDialog);
	aDialog.updateLayout();
	this.dialogs.push(aDialog);
	
	this.refresh();
}

core.Desktop.prototype.resize = function(aX, aY, aWidth, aHeight){
	if(this.dDiv){
		$(this.dDiv).width(aWidth);
		$(this.dDiv).height(aHeight);
	}
	for(var i = 0; i<this.dialogs.length; i++){
		var d = this.dialogs[i];
		d.show();
		var minWidth = d.getMinWidth();
		var width = d.maxWidth;
		if(width<minWidth)
			width = minWidth;
		if(width>aWidth)
			width = aWidth;
		d.setWidth(width);
		var minHeight = d.getMinHeight();
		var height = d.maxHeight;
		if(height<minHeight)
			height = minHeight;
		if(height>aHeight)
			height = aHeight;
		d.setHeight(height);
		d.resize(Math.floor((aWidth-width)/2), Math.floor((aHeight-height)/2), width, height);
	}
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
}

core.SplittedPanel = function(config){
	this.init(config);
};

core.extendClass('core.SplittedPanel', 'core.Box');

core.SplittedPanel.prototype.processKeyDown = function(e){
	if(this.leftElement && !this.leftElement.processKeyDown(e))
		return false;	
	if(this.rightElement && !this.rightElement.processKeyDown(e))
		return false;
	return true;
}
core.SplittedPanel.prototype.replaceChild = function(aOldChild, aChild, aDestroyChild){
	if(!aDestroyChild)
		aDestroyChild = core.REPLACE_CHILD_HIDE;
	if(aOldChild==this.leftElement){
		if(aDestroyChild==core.REPLACE_CHILD_DESTROY)
			this.leftElement.destroy();
		if(aDestroyChild==core.REPLACE_CHILD_HIDE)
			this.leftElement.hide();
		this.setLeftElement(aChild);
	}
	if(aOldChild==this.rightElement){
		if(aDestroyChild==core.REPLACE_CHILD_DESTROY)
			this.rightElement.destroy();
		if(aDestroyChild==core.REPLACE_CHILD_HIDE)
			this.rightElement.hide();
		this.setRightElement(aChild);
	}
}
core.SplittedPanel.prototype.updateLayout = function(){
	if(this.leftElement)
		this.leftElement.updateLayout();
	if(this.rightElement)
		this.rightElement.updateLayout();
}

core.SplittedPanel.prototype.stopResize = function(aFixResults, aX, aY){
	$(this.moveLayer).remove();
	if(aFixResults){
		if(this.horizontal)
			this.divider = (this.leftSize-(this.mouseY-aY))/this.bounds.height;
		else
			this.divider = (this.leftSize-(this.mouseX-aX))/this.bounds.width;
	}
	this.resize(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height);
}

core.SplittedPanel.prototype.moveDivider = function(aX, aY){
	if(this.horizontal){
//		console.log('mouseCoords2: ', aX, aY);
		var newCoord = this.leftSize-(this.mouseY-aY);
		if(newCoord<0)
			newCoord = 0;
		if(newCoord+SPLITTER_WIDTH>this.bounds.height)
			newCoord = this.bounds.height-SPLITTER_WIDTH;
		$(this.dividerDiv).css('top', newCoord);
	}else{
		var newCoord = this.leftSize-(this.mouseX-aX);
		if(newCoord<0)
			newCoord = 0;
		if(newCoord+SPLITTER_WIDTH>this.bounds.width)
			newCoord = this.bounds.width-SPLITTER_WIDTH;
		$(this.dividerDiv).css('left', newCoord);
	}
}

core.SplittedPanel.prototype.startMoveDivider = function(){
//	console.log('mouseCoords: ', this.mouseX, this.mouseY);
	$(this.dividerDiv).appendTo(this.div);
	this.moveLayer = core.newDiv(this.div, 'split_layer');
	$(this.moveLayer).width(this.bounds.width);
	$(this.moveLayer).height(this.bounds.height);
	$(this.moveLayer).css('cursor', this.horizontal?'row-resize':'col-resize');
	$(this.moveLayer).fadeTo(1, 0.1, function(){
		$(this).show();
	});
	var instance = this;
	$(this.moveLayer).mousemove(function(e){
//		console.log('mousemove', e);
		instance.moveDivider(e.clientX, e.clientY);
	});
	$(this.moveLayer).mouseout(function(e){
		instance.stopResize();
		return false;
	});
	$(this.moveLayer).mouseup(function(e){
//		console.log('mouseup', e);
		instance.stopResize(true, e.clientX, e.clientY);
		return false;
	});
}

core.SplittedPanel.prototype.init = function(config){
	this.xType = 'sppanel';
	this.superCall('init', arguments, config);
	var instance = this;
	this.divider = config.divider || 0.5;
	this.horizontal = !config.vertical;
	this.dividerDiv = core.newDiv(this.div, 'splitter');
	$(this.dividerDiv).css('cursor', this.horizontal?'row-resize':'col-resize');
	if(config.leftElement)
		this.setLeftElement(config.leftElement);
	if(config.rightElement)
		this.setRightElement(config.rightElement);
	$(this.dividerDiv).mousedown(function(e){
		instance.mouseX = e.clientX;
		instance.mouseY = e.clientY;
		instance.startMoveDivider();
		return false;
	});
};

core.SplittedPanel.prototype.getNeighbour = function(aElement){
	if(aElement==this.leftElement)
		return this.rightElement;
	if(aElement==this.rightElement)
		return this.leftElement;
	return null;
}

core.SplittedPanel.prototype.setLeftElement = function(aElement){
	this.leftElement = aElement;
	if(aElement)
		this.reParent(aElement);
}

core.SplittedPanel.prototype.setRightElement = function(aElement){
	this.rightElement = aElement;
	if(aElement)
		this.reParent(aElement);
}

core.SplittedPanel.prototype.getDropTarget = function(aDraggable, aX, aY){
	if(this.horizontal){
		if(this.leftElement && aY<=this.leftSize)
			return this.leftElement.getDropTarget(aDraggable, aX, aY);
		if(this.rightElement && aY>=this.leftSize+SPLITTER_WIDTH);
			return this.rightElement.getDropTarget(aDraggable, aX, aY-this.leftSize-SPLITTER_WIDTH);
	}else{
		if(this.leftElement && aX<=this.leftSize)
			return this.leftElement.getDropTarget(aDraggable, aX, aY);
		if(this.rightElement && aX>=this.leftSize+SPLITTER_WIDTH);
			return this.rightElement.getDropTarget(aDraggable, aX-this.leftSize-SPLITTER_WIDTH, aY);		
	}
	return null;
}

core.SplittedPanel.prototype.resize = function(aX, aY, aWidth, aHeight){
//	core.log('resize', this.className, this.instanceID, aX, aY, aWidth, aHeight);
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	var sizeToDivide = this.horizontal?aHeight:aWidth;
	if(this.divider>1)
		this.divider = 1;
	if(this.divider<=0)
		this.divider = 0;
	var leftSize = Math.floor((sizeToDivide)*this.divider);
	var rightSize = sizeToDivide-SPLITTER_WIDTH-leftSize;
	if(leftSize+SPLITTER_WIDTH>sizeToDivide)
		leftSize = sizeToDivide-SPLITTER_WIDTH;
	if(rightSize+SPLITTER_WIDTH>sizeToDivide)
		rightSize = sizeToDivide-SPLITTER_WIDTH;
	this.leftSize = leftSize;
	if(this.horizontal){
		if(this.leftElement){
			this.leftElement.doResize(0, 0, aWidth, leftSize);
		}
		if(this.rightElement){
			this.rightElement.doResize(0, leftSize+SPLITTER_WIDTH, aWidth, rightSize);
		}
		$(this.dividerDiv).css('left', 0);
		$(this.dividerDiv).css('top', leftSize);
		$(this.dividerDiv).width(aWidth);
		$(this.dividerDiv).height(SPLITTER_WIDTH);
	}else{
		if(this.leftElement){
			this.leftElement.doResize(0, 0, leftSize, aHeight);
		}
		if(this.rightElement){
			this.rightElement.doResize(leftSize+SPLITTER_WIDTH, 0, rightSize, aHeight);
		}
		$(this.dividerDiv).css('left', leftSize);
		$(this.dividerDiv).css('top', 0);
		$(this.dividerDiv).width(SPLITTER_WIDTH);
		$(this.dividerDiv).height(aHeight);		
	}
}

core.Dialog = function(config){
	this.init(config);
}

core.extendClass('core.Dialog', 'core.Box');

core.Dialog.prototype.init = function(config){
	this.xType = 'dialog';
	this.title = new core.OneLineHelper({
		text: config.title || 'Untitled dialog:',
		cls: 'dialog_title',
		parent: this
	});
	this.title.minHeight = 20;
	this.body = new core.Panel({
		rows: config.rows,
		columns: config.columns,
		items: config.items
	});
	this.reParent(this.body);
	
	this.superCall('init', arguments, {
		columns: '1',
		rows: 'd, 1',
		items: [this.title, this.body]
	});
	
	this.align = config.align || 'center';
	this.valign = config.align || 'middle';
	this.maxWidth = config.maxWidth || 0;
	this.maxHeight = config.maxHeight || 0;
	$(this.div).addClass('dialog');
}
